package com.mew.chess.core.game.dyj.game.core;

import cn.hutool.core.util.RandomUtil;
import com.mew.chess.core.game.bjl.game.BjlRoomManager;
import com.mew.chess.core.game.bjl.game.common.BjlBetType;
import com.mew.chess.core.game.bjl.game.common.BjlConstantValue;
import com.mew.chess.core.game.bjl.game.common.BjlResultBO;
import com.mew.chess.core.game.bjl.game.core.BjlCardDevice;
import com.mew.chess.core.game.bjl.game.core.BjlCardUtil;
import com.mew.chess.core.game.bjl.game.core.BjlGameUtil;
import com.mew.chess.core.game.bjl.game.message.PushOpenCardMessage;
import com.mew.chess.core.game.dyj.game.DyjRoomManager;
import com.mew.chess.core.game.dyj.game.common.DyjBetType;
import com.mew.chess.core.game.dyj.game.common.DyjResultBO;
import com.mew.chess.core.game.pokdeng.game.core.CardUtil;
import com.mew.common.enm.UserType;
import com.mew.common.util.NumberUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author dylin
 * @create 2021/5/10
 */
public class DyjDiceUtil {
    public static final int DICE_NUMBER=3;//骰子数量
    /**
     * 摇骰子
     * @param rbo
     */
    public static void rockDice(DyjResultBO rbo){
        for (int i = 0; i < DICE_NUMBER; i++) {
            rbo.getDices().add(RandomUtil.randomInt(1,7));
        }
        computeWin(rbo);
    }

    /**
     * 计算赢得结果
     * @param rbo
     */
    private static void computeWin(DyjResultBO rbo){
        rbo.getWinList().add(isBig(rbo.getDices())?DyjBetType.Big.code():DyjBetType.Small.code());
        int type=diceType(rbo.getDices());
        if(type>0){
            rbo.getWinList().add(type);
        }
    }

    /**
     * 判断大小
     * @param dices
     * @return
     */
    private static boolean isBig(List<Integer> dices){
        int allVal=0;
        for (Integer dice : dices) {
            allVal+=dice;
        }
        return allVal>10;
    }

    /**
     * 判断类型
     */
    private static int diceType(List<Integer> dices){
        if(dices.size()!=DICE_NUMBER){
            return 0;
        }

        if(dices.get(0).equals(dices.get(1))&&dices.get(1).equals(dices.get(2))){
            return DyjBetType.Leopard.code();
        }
        if(dices.get(0).equals(dices.get(1))||dices.get(1).equals(dices.get(2))||dices.get(0).equals(dices.get(2))){
            return DyjBetType.Pair.code();
        }
        List<Integer> tc=new ArrayList<>();
        tc.addAll(dices);
        tc.sort(Integer::compareTo);
        if(tc.get(0)+1==tc.get(1)&&tc.get(1)+1==tc.get(2)){
            return DyjBetType.Straight.code();
        }
        return 0;
    }

    /**
     * 计算区域压注
     */
    public static Map<Integer,Long> computeRegionBet(DyjRoomManager roomManager){
        Map<Integer,Long> regionBetMap = new HashMap<>();
        roomManager.getPlayerList().values().stream().filter(u -> u.userType != UserType.Robot.code() && u.getAllBet() > 0).forEach(u -> {
            for (Map.Entry<Integer, long[]> entry : u.getBetRecord().entrySet()) {
                Long allBet = regionBetMap.get(entry.getKey());
                allBet = allBet == null ? 0l : allBet;
                regionBetMap.put(entry.getKey(), allBet+entry.getValue()[0]);
            }
        });
        return regionBetMap;
    }

    public static void main(String[] args) {
        int time=1000000;
        int big=0;
        int small=0;
        int pair=0;
        int straight=0;
        int leopard=0;
        for (int i = 0; i < time; i++) {
            DyjResultBO resultBO=new DyjResultBO();
            DyjDiceUtil.rockDice(resultBO);
            if(resultBO.getWinList().contains(DyjBetType.Big.code())){
                big++;
            }
            if(resultBO.getWinList().contains(DyjBetType.Small.code())){
                small++;
            }
            if(resultBO.getWinList().contains(DyjBetType.Pair.code())){
                pair++;
            }
            if(resultBO.getWinList().contains(DyjBetType.Straight.code())){
                straight++;
            }
            if(resultBO.getWinList().contains(DyjBetType.Leopard.code())){
                leopard++;
            }
        }
        System.out.println("总次数："+time);
        System.out.println("大："+big);
        System.out.println("小："+small);
        System.out.println("对子："+pair);
        System.out.println("顺子："+straight);
        System.out.println("豹子："+leopard);
    }
}
